﻿using Api.Application.Command.Province;
using Api.DTO;
using AutoMapper;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;

namespace Api.Application.Handler.Province
{
    public class ShowProvinceHandler : IRequestHandler<ShowProvinceCommand, ApiPaging<ProvinceDTO>>
    {
        private readonly IBase<ProvinceModel> proinceRepo;
        private readonly IMapper mapper;

        public ShowProvinceHandler(IBase<ProvinceModel> proinceRepo, IMapper mapper)
        {
            this.proinceRepo = proinceRepo;
            this.mapper = mapper;
        }
        public Task<ApiPaging<ProvinceDTO>> Handle(ShowProvinceCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<ProvinceDTO> paging = new ApiPaging<ProvinceDTO>();
            var province = proinceRepo.GetAll().Where(x => x.IsDeleted == false).ToList();
            var provinces = mapper.Map<List<ProvinceDTO>>(province);
            paging.Code = ApiEnum.OK;
            paging.Msg = "获取省份成功";
            paging.Data = provinces;
            return Task.FromResult(paging);
        }
    }
}
